package com.zsrd.self.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsrd.self.base.Result;
import com.zsrd.self.entity.SelfCurrType;
import com.zsrd.self.entity.SelfUser;
import com.zsrd.self.service.SelfCurrTypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xxx
 * @since 2024-04-01
 */
@RestController
@RequestMapping("/selfCurrType")
public class SelfCurrTypeController {
    @Autowired
    private SelfCurrTypeService selfCurrTypeService;
    //为前端方便显示，数据并不一定是该id下数据，可能为其他id数据，保证固定格式
    @PostMapping("/typeList")
    public Result typeList(@RequestBody  SelfCurrType selfCurrType){
        Integer level = selfCurrType.getLevel();
//        List<SelfCurrType> startList = null;
//        SelfCurrType endList = null;
//        if(!Objects.isNull(level) && 0 != selfCurrType.getId()  ){
//            startList = selfCurrTypeService.list(new QueryWrapper<SelfCurrType>().eq("pid", 0).orderByAsc("sort"));
//            setChildren(startList,selfCurrType.getLevel());
//             endList = getEndList(startList);
//        }
        List<SelfCurrType> list = selfCurrTypeService.list(new QueryWrapper<SelfCurrType>().eq(StringUtils.isEmpty(selfCurrType.getId()),"pid", 0).eq(!StringUtils.isEmpty(selfCurrType.getId()),"pid",selfCurrType.getId()).orderByAsc("sort"));
        if(!CollectionUtils.isEmpty(list)){
            setChildren(list,null);
        }
//        if(!CollectionUtils.isEmpty(startList)){
//            endList.setChildren(list);
//        }else {
//            startList =list;
//        }
        return Result.success(list);
    }
    private List<SelfCurrType> setChildren(List<SelfCurrType> list, Integer level){

        if(!CollectionUtils.isEmpty(list)){
//            if(!Objects.isNull(level) && level == list.get(0).getLevel()){
//                return list;
//            }
            list.stream().forEach(u->{
                List<SelfCurrType> children = selfCurrTypeService.list(new QueryWrapper<SelfCurrType>().eq("pid", u.getId()).orderByAsc("sort"));
                u.setChildren(children);
                setChildren(children,level);
            });
        }else{
            return  list;
        }
        return  list;
    }
    private SelfCurrType getEndList(List<SelfCurrType> list){
        if(!CollectionUtils.isEmpty(list)){
            List<SelfCurrType> children = list.get(0).getChildren();
            if(!CollectionUtils.isEmpty(children)){
                return  getEndList(children);
            }else{
                return list.get(0);
            }
        }
        return list.get(0);
    }
}

